#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_set>
#include<bits/stdc++.h>
#include<ext/pb_ds/priority_queue.hpp>
//#include<bits/extc++.h>
using namespace std;
//using namespace __gnu_pbds;

#define fi first
#define se second
#define SIZE(x) ((int)x.size())
#define out(x) cerr<<#x<<" = "<<(x)<<endl;
#define MP(a,b) make_pair(a,b)
typedef long long ll;
#define VI vector<int>
#define VVI vector< vector<int> >
#define VL vector<ll>
#define VVL vector<VL >
#define VC vector<char>
#define PI pair<int,int>
#define PL pair<ll,ll>
#define EPS 0.0000001


inline int lson(int x){return x<<1;}
inline int rson(int x){return (x<<1)|1;}

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

template<typename T> 
inline void read(T &x){
	x=0;T f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

//#define INF 5340172838076674L
#define INF 10000000
//#define MOD 998244353
//#define W 20

int ch[25*100005][2];
int sum[25*100005];
int tot=1;

//c=1表示插入，c=-1表示删除
void ins(int x,int c){
	int p=1,tmp;
	for(int i=25;i>=0;i--){
		tmp=(x>>i)&1;
		if(ch[p][tmp]==0){
			p=ch[p][tmp]=++tot;
		}else p=ch[p][tmp];
		sum[p]+=c;
	}
}
//查询第x个元素，0是-INF
int kth(int x){
	int p=1,tmp,res=0;
	for(int i=25;i>=0;i--){
		if(x>sum[ch[p][0]]){
			res|=1<<i;
			x-=sum[ch[p][0]];
			p=ch[p][1];
		}else p=ch[p][0];
	}
	return res;
}
//查询x元素排名
int rk(int x){
	int res=0,tmp,p=1;
	for(int i=25;i>=0;i--){
		tmp=(x>>i)&1;
		res+=tmp*sum[ch[p][0]];
		p=ch[p][tmp];
	}
	return res;
}

int pre(int x){return kth(rk(x));}
int nex(int x){return kth(rk(x+1)+1);}

void dfs(int x,int v,int d){
	if(d==-1){
		cout<<(v-INF)<<' ';
	}
	if(ch[x][0])dfs(ch[x][0],v,d-1);
	if(ch[x][1])dfs(ch[x][1],v|(1<<d),d-1);
}

//01Trie
int main() {
	#ifdef BUG
    freopen("a.in","r",stdin);
    #endif
	memset(ch,0,sizeof ch);
	memset(sum,0,sizeof sum);
	int n=read();
	int a,b;
	while(n--){
		//if(a==1||a==2){cout<<"a  ";dfs(1,0,25);cout<<endl;}
		a=read();
		if(a==1)ins(read()+INF,1);
		else if(a==2)ins(read()+INF,-1);
		else if(a==3)printf("%d\n",rk(read()+INF)+1);
		else if(a==4)printf("%d\n",kth(read())-INF);
		else if(a==5)printf("%d\n",pre(read()+INF)-INF);
		else if(a==6)printf("%d\n",nex(read()+INF)-INF);
	}
}


/*
//放一个带佬写的01Trie

#include <bits/stdc++.h>
using namespace std;
const int maxn=110000*35;
const int inf=10000000;
int ch[maxn][2]={},sum[maxn]={},tot=1;
int n,cmd,x;
void ins(int val,int c=1) {
    for(int i=31,t,rt=1;(t=(val>>i)&1),~i;i--){
        if(ch[rt][t]==0)ch[rt][t]=++tot;
        sum[rt=ch[rt][t]]+=c;
    }
}
int kth(int val,int ret=0) {
    for(int i=31,t,rt=1;(t=(val>>i)&1),~i;i--){
        if(val>sum[ch[rt][0]]){
            val-=sum[ch[rt][0]];
            ret|=1<<i;
            rt=ch[rt][1];
        }else rt=ch[rt][0];
    }
    return ret;
}
int rk(int val,int ret=0){
    for(int i=31,t,rt=1;(t=(val>>i)&1),~i;i--){
        ret+=t*sum[ch[rt][0]];
        rt=ch[rt][t];
    }
    return ret;
}
int pre(int val){return kth(rk(val));}
int sub(int val){return kth(rk(val+1)+1);}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
    	scanf("%d%d",&cmd,&x);
    	x+=inf;
    	if(cmd==1)ins(x);
    	if(cmd==2)ins(x,-1);
    	if(cmd==3)printf("%d\n",rk(x)+1);
    	if(cmd==4)printf("%d\n",kth(x-inf)-inf);
		if(cmd==5)printf("%d\n",pre(x)-inf);
		if(cmd==6)printf("%d\n",sub(x)-inf);
	}
}

 */